//
//  HGCollectionViewCell.m
//  中关村在线
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "HGCollectionViewCell.h"
#import "NetWorkHandler.h"
#import "News.h"
#import "HGDB.h"
#import "Picture.h"
#import "UIImageView+WebCache.h"
#import "THreePICTableViewCell.h"
#import "HG3TableViewCell.h"
#import "HG4TableViewCell.h"
#import "HG5TableViewCell.h"
#import "HG6TableViewCell.h"


@implementation HGCollectionViewCell

- (void)dealloc{
    
   
    [_imageView release];
    [_smallScrollView release];
    [_arr release];
    [_arrPic release];
    [_tableView release];
    [_tableView2 release];
    [_indexPath1 release];
    Block_release(_block);
    
    [super dealloc];
}



- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self handleData];
    }
    return self;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    News *model = _arr[indexPath.row];
    
    
    [self.delegate TiaoZhuanWith:model];
    
}


- (void)initSmallScrollView{
    
    
    self.smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)];
    
    _smallScrollView.backgroundColor = [UIColor redColor];
    _smallScrollView.contentSize = CGSizeMake(375 *_arrPic.count , 200);
    _smallScrollView.pagingEnabled = YES;
    _smallScrollView.bounces = NO;
    _smallScrollView.delegate = self;
    [self addSubview:_smallScrollView];
    
    NSLog(@"%@", _arrPic);
    [_smallScrollView release];
}

//滚动视图滑动响应问题
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"1");
    if (_smallScrollView.contentOffset.x == 375 * 4) {
        
        _smallScrollView.userInteractionEnabled = NO;
        NSLog(@"2");
        
    }
    
    if (_indexPath1.row == 3 || _indexPath1.row == 4 || _indexPath1.row == 7) {
        if (_smallScrollView.contentOffset.x == 375 * 2) {
            
            _smallScrollView.userInteractionEnabled = NO;
            NSLog(@"2");
            
        }
    }
    
    
    
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    News *model = _arr[indexPath.row];
    
    NSArray *arr = [NSArray array];
    arr = model.pics;
    
    
    NSLog(@"picadfadsf%@", arr);
    
    
    
            
    
    
        
        if (self.indexPath1.row == 3) {
            
            tableView = _tableView2;
            
            if (arr.count == 3) {
                
                HG4TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HG4cell"];
                
                [cell.image1 sd_setImageWithURL:[NSURL URLWithString:arr[0]]];
                [cell.image2 sd_setImageWithURL:[NSURL URLWithString:arr[1]]];
                [cell.image3 sd_setImageWithURL:[NSURL URLWithString:arr[2]]];
                
                cell.titleLabel.text = model.stitle;
                NSString *str = [model.sdate substringWithRange:NSMakeRange(11, 5)];
                cell.timeLabel.text = str;
                
                
                return cell;
            }else{
                
                HG3TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HG3cell"];
                
                
                [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
                
                cell.titleLabel.text = model.stitle;
                
                
                NSString *str = [model.sdate substringWithRange:NSMakeRange(11, 5)];
                
                cell.timeLabel.text = str;
                
                
                return cell;
                
            }
        
       
        } else if (_indexPath1.row == 10){
            
            tableView = _tableView;
            if (model.pics.count != 0) {
                
                THreePICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
                
                [cell.image1 sd_setImageWithURL:[NSURL URLWithString:arr[0]]];
                [cell.image2 sd_setImageWithURL:[NSURL URLWithString:arr[1]]];
                [cell.image3 sd_setImageWithURL:[NSURL URLWithString:arr[2]]];
                
                cell.TitleLablt.text = model.stitle;
                NSString *str = [model.sdate substringWithRange:NSMakeRange(6, 4)];
                cell.TimeLabel.text = str;
                
                
                return cell;
            }else{
                
                HG5TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HG5cell"];
                
                
                [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
                
                cell.titleLabel.text = model.stitle;
                
                
                NSString *str = [model.sdate substringWithRange:NSMakeRange(6, 4)];
                
                cell.dateLabel.text = str;
                cell.image2.image = [UIImage imageNamed:@"iconfont-shipin.png"];
                cell.numberLabel.text = model.video_time;
                cell.FLabel.text = model.tag;
                cell.numbenLabel.text = [model.play_count stringValue];
                
                return cell;
                
            }
            
            
            
            
            
        }else if (_indexPath1.row == 14){
            
            tableView = _tableView;
            if (model.pics.count == 1) {
                
                HG6TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HG6cell"];
                
                [cell.image sd_setImageWithURL:[NSURL URLWithString:arr[0]]];
               ;
                
                cell.titleLabel.text = model.stitle;
                NSString *str = [model.sdate substringWithRange:NSMakeRange(6, 4)];
                cell.timeLabel.text = str;
                cell.number.text = model.live_num;
                
                return cell;
            }else{
                
                HGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
                
                
                [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
                
                cell.label1.text = model.stitle;
                
                
                NSString *str = [model.sdate substringWithRange:NSMakeRange(6, 4)];
                
                cell.TimeLabel.text = str;
                
                return cell;
                
            }

            
            
        }else {
            
            tableView = _tableView;
            if (model.pics.count != 0) {
                
                THreePICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
                
                [cell.image1 sd_setImageWithURL:[NSURL URLWithString:arr[0]]];
                [cell.image2 sd_setImageWithURL:[NSURL URLWithString:arr[1]]];
                [cell.image3 sd_setImageWithURL:[NSURL URLWithString:arr[2]]];
                
                cell.TitleLablt.text = model.stitle;
                NSString *str = [model.sdate substringWithRange:NSMakeRange(6, 4)];
                cell.TimeLabel.text = str;
                cell.PingLunLabel.text = [NSString stringWithFormat:@"%@", model.comment_num];
                return cell;
            }else{
                
                HGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
                
                
                [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
                
                cell.label1.text = model.stitle;
                
                
                NSString *str = [model.sdate substringWithRange:NSMakeRange(6, 4)];
                
                cell.TimeLabel.text = str;
                
                return cell;
                
            }

        }
    
    
    
    
    
    
    
    
    
   
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    News *model = _arr[indexPath.row];
    
    NSLog(@"454654654%@", model.pics);
    
    
    if (tableView == _tableView2) {
        
        
        if (model.pics.count >= 3) {
            return 120;
        }else{
            
            return 80;
        }
    }
    
//    if (tableView == _tableView) {
//        if (model.pics.count == 3) {
//            
//            return 120;
//        }else{
//            
//        }
//        return 80;
//        
//    }
    
    return 120;
    
}

//创建tableView
- (void)initTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 25, 375, 560) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self addSubview:_tableView];
    
    [_tableView release];
    
    
    
    //注册
    [_tableView registerNib:[UINib nibWithNibName:@"HGTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    
    [_tableView registerNib:[UINib nibWithNibName:@"THreePICTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell1"];
    
    [_tableView registerNib:[UINib nibWithNibName:@"HG5TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HG5cell"];
    
    [_tableView registerNib:[UINib nibWithNibName:@"HG6TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HG6cell"];
    
}

//创建tableView2

- (void)initTableView2{
    
    
    self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 25, 375, 560) style:UITableViewStylePlain];
    
    _tableView2.delegate = self;
    _tableView2.dataSource = self;
    
    [self addSubview:_tableView2];
    
    
    [_tableView2 release];
    //主册
    
    [_tableView2 registerNib:[UINib nibWithNibName:@"HG3TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HG3cell"];
    
    
    [_tableView2 registerNib:[UINib nibWithNibName:@"HG4TableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HG4cell"];
    
    
}






//数据处理
- (void)handleData{
    
    //block传值  将网址字符串传进来
    self.block = ^(NSString *str){
        
        
        [NetWorkHandler GETRequestWithString:str completion:^(id result) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSData *data = result;
                //Json解析
                NSDictionary *a = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                //得到双优数据的字典
                NSArray *arr = a[@"list"];
                
               
                self.arr = [NSMutableArray array];
                    //遍历数据数组 将字典转化为model
                for (NSDictionary *dic in arr) {
                   
                    
                    News *model = [News new];
                    
                    [model setValuesForKeysWithDictionary:dic];
                    
                    
                    [_arr addObject:model];
                    
                    
                }
                
                //把这两步放在这里 因为网络有延迟
                //创建tableview
                
                [self initTableView];
                [self initTableView2];
                
                
                NSMutableArray *arr1 = a[@"pics"];
                
                self.arrPic = [NSMutableArray array];
                
                for (NSDictionary *dic in arr1) {
                    
                    Picture *model = [Picture picture];
                    [model setValuesForKeysWithDictionary:dic];
                    
                    [_arrPic addObject:model];
                }
                NSLog(@"图片%@", _arrPic);
                
                if (_arrPic.count != 0) {
                    
                    [self initSmallScrollView];
                    _tableView.tableHeaderView = _smallScrollView;
                   
                    _tableView2.tableHeaderView = _smallScrollView;
                    
                    for (NSInteger i = 1; i <= arr1.count; i++) {
                        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((i - 1) * 375, 0, 375, 200)];
                        
                        Picture *model =  [_arrPic objectAtIndex:i - 1];
                        [image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
                        [_smallScrollView addSubview:image];
                        
                        UIView *view = [[UIView alloc] initWithFrame:CGRectMake((i - 1) * 375, 175, 375, 25)];
                        view.backgroundColor = [UIColor blackColor];
                        view.alpha = 0.1;
                        [_smallScrollView addSubview:view];
                        [view release];
                        
                        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake( (i - 1)* 375 + 6, 179, 375, 18)];
                        label.text = model.stitle;
                        label.textColor = [UIColor whiteColor];
                        
                        [_smallScrollView addSubview:label];
                        
                        [image release];
                        
                    }
                    
                }
                
                
                
                
                
                
            });
            
            
            
        }];
        
    };
    
    
    
    

}







@end
